﻿using System;
using System.Web;
using System.IO;
using System.Text;
using System.Data;
using System.Xml;
using System.Web.SessionState;
using System.Web.Services;
using Gift.WebApp;
using System.Collections;

using Gift.DAL;
using Gift.Model;

namespace WebApp.cn.ashx
{
    public class registerHandler : IHttpHandler, IRequiresSessionState
    {



        protected string remindSetXmlPath = "/xml/remind_set.xml";

        BasePage thePage = new BasePage();
        tbUser oUser = new tbUser();

        /// <summary>
        /// 您将需要在您网站的 web.config 文件中配置此处理程序，
        /// 并向 IIS 注册此处理程序，然后才能进行使用。有关详细信息，
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members


        public bool IsReusable
        {
            // 如果无法为其他请求重用托管处理程序，则返回 false。
            // 如果按请求保留某些状态信息，则通常这将为 false。
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {

            if (string.Compare(context.Request.Form["code"], context.Session["rCheckCode"].ToString(), true) != 0)
            {
                context.Response.Write("0");
            }
            else if (!BaseFactory.theUser.DetectionUser(context.Request.Form["userName"]))
            {
                context.Response.Write("1");
            }
            else
            {
                this.oUser.userName = context.Request.Form["userName"];
                this.oUser.userPwd = Gift.Security.DESEncrypt.Encrypt(context.Request.Form["userPwd"].Trim());
                this.oUser.email = context.Request.Form["email"];
                DataTable defaultUserRank = BaseFactory.theUserRank.GetDefaultUserRank();
                if (defaultUserRank.Rows.Count > 0)
                {
                    this.oUser.userRank = Convert.ToInt32(defaultUserRank.Rows[0]["id"]);
                    this.oUser.RankName = defaultUserRank.Rows[0]["rankName"].ToString();
                }
                this.oUser.registerIP = new ClinetMAC().GetIPAddress();
                this.oUser.money = 0;
                this.oUser.payPoints = 0;
                this.oUser.id = BaseFactory.theUser.UserRegister(this.oUser);
                if (this.oUser.id != 0)
                {
                    string str2;
                    string str3;
                    XmlDocument document = new XmlDocument();
                    document.Load(HttpContext.Current.Server.MapPath(this.remindSetXmlPath));
                    XmlElement element = document.SelectSingleNode("set") as XmlElement;
                    XmlElement element2 = document.SelectSingleNode("set").ChildNodes[0] as XmlElement;
                    if (element2.GetAttribute("mail_remind") == "true")
                    {
                        SendMailMessage message = new SendMailMessage();
                        string str = File.ReadAllText(context.Server.MapPath("/cn/txt_template/member_register.txt"));
                        message.subject = "会员注册";
                        message.body = str.Replace("[userName]", this.oUser.userName).Replace("[password]", context.Request.Form["userPwd"].Trim()).Replace("email", this.oUser.email);
                        message.html = true;
                        DataTable allEnabledMail = BaseFactory.theMail.GetAllEnabledMail();
                        Random random = new Random();
                        DataRow row = allEnabledMail.Rows[random.Next(allEnabledMail.Rows.Count - 1)];
                        message.from = row["fromAddress"].ToString();
                        message.fromName = row["fromName"].ToString();
                        message.mailDomain = row["mailDomain"].ToString();
                        message.mailDomainPort = Convert.ToInt32(row["mailDomainPort"]);
                        message.mailServerUserName = row["mailServerUserName"].ToString();
                        message.mailServerPassWord = row["mailServerPassWord"].ToString();
                        if (element2.GetAttribute("notice_member") == "true")
                        {
                            message.mailTo = this.oUser.email;
                            message.RuleSend();
                        }
                        if (element2.GetAttribute("notice_admin") == "true")
                        {
                            message.mailTo = element.GetAttribute("remind_email");
                            message.RuleSend();
                        }
                    }
                    context.Session["user"] = this.oUser;
                    if (HttpContext.Current.Request.Cookies["shopping_cart_mac"] != null)
                    {
                        str2 = HttpContext.Current.Request.Cookies["shopping_cart_mac"].Value;
                        str3 = this.oUser.id.ToString();
                        BaseFactory.theShoppingCart.ModifyShoopingCartUser(str2, str3);
                    }
                    if (HttpContext.Current.Request.Cookies["group_buy_shopping_cart_mac"] != null)
                    {
                        str2 = HttpContext.Current.Request.Cookies["group_buy_shopping_cart_mac"].Value;
                        str3 = this.oUser.id.ToString();
                        BaseFactory.theGroupBuyShoppingCart.ModifyGroupBuyShoopingCartUser(str2, str3);
                    }
                    int inBoxNoIsReadByAddressee = BaseFactory.theInBox.GetInBoxNoIsReadByAddressee(thePage.LoginUserMember.ClientUser.userName);
                    context.Response.Write(string.Concat(new object[] { "您好，<span class=\"red\">", thePage.LoginUserMember.ClientUser.userName, "</span>，欢迎光临 <strong>MICRORNET商城</strong>！&nbsp;<a href=\"/cn/member_center/my_order.html\">[&nbsp;会员中心&nbsp;]</a>&nbsp;<a class=\"mail\" href=\"/cn/member_center/in_box.html\">(<strong>", inBoxNoIsReadByAddressee, "</strong>)</a>&nbsp;<a href=\"userExitHandler.ashx\">[&nbsp;退出&nbsp;]</a>" }));
                }
                else
                {
                    context.Response.Write("2");
                }
            }
        }

        #endregion
    }
}
